<?php

namespace support;

use support\exception\ExceptionHandler;
use think\facade\Cache;
use think\facade\Db;

class App
{
    public function init()
    {
        // 加载基础组件
        $cacheConfig = config('cache');
        Cache::config($cacheConfig);
        $dbConfig = config('database');
        Db::setConfig($dbConfig);
        $middleware = config('middleware', []);
    }

    public function run(): Response
    {

        // 执行前置中间件
        $request = request();
        try {
            $reponse = $this->handle($request);
        } catch (\Throwable $e) {
            $exceptionHandler = app(ExceptionHandler::class);
            $exceptionHandler->report($e);
            $reponse = $exceptionHandler->render($request, $e);
        }

        return $reponse;
    }

    public function handle(Request $request)
    {
        $route = new Route();
        return $route->dispatch();
    }

    public function end()
    {
        // 执行后置中间件
    }
}
